<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-11-27
 */
namespace app\index\controller;
/**
 * 文章评论
 * @author Administrator
 *
 */
class CommentController extends BaseController{
	/**
	 * 添加评论
	 */
	public function addCommentOp(){
		$result = array();
		$p_data = input('param.');
		$data['article_id'] = $p_data['article_id'];
		$data['content'] = $p_data['comment'];
		$data['member_id'] = session('member_id');
		$res = model('Comment')->insertData($data);
		if($res !== false){
			$comment_info = model('Comment')->getInfo(array('id'=>$res),'id,member_id,content,add_time');
			$member_info = model('Member')->getInfo(array('id'=>$comment_info['member_id']),'nick_name,head_imgurl');
			//网站配置信息
			$website_info = model('config')->getConfig(array('type'=>'website'));
			$result['error'] = 0;
			$result['member_info'] = $member_info;
			$result['website_info'] = $website_info;
			$comment_info['add_time'] = date('Y-m-d H:i:s',$comment_info['add_time']);
			$comment_info['content'] = setHtmlContent($comment_info['content']);
			$result['comment_info'] = $comment_info;
		}else{
			$result['msg'] = '不好意思，评论丢了！';
			$result['error'] = 1;
		}
		return json($result);
	}
	/**
	 * 获取更多评论
	 */
	public function getCommentOp(){
		$result = array();
		//文章编号
		$article_id = input('post.article_id');
		//页数
		$page = input('post.page');
		$condition = array();
		$list = model('Comment')->getList("c.article_id=".$article_id." and c.status = 1 and c.type = 1 and c.is_del = 0",$page);
		foreach ($list as $k=>$v){
			$list[$k]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
			$list[$k]['content'] = setHtmlContent($v['content']);
		}
		$result['list'] = $list;
		//获取评论条数
		$comment_count = model('Comment')->getCount("article_id=".$article_id." and status = 1 and type = 1 and is_del = 0");
		if($comment_count > $page*10){
			$result['is_exist'] = 1;
		}else{
			$result['is_exist'] = 0;
		}
		$website_info = model('config')->getConfig(array('type'=>'website'));
		$result['article_id'] = $article_id;
		$result['website_info'] = $website_info;
		$result['page'] = $page;
		return json($result);
	}
	/**
	 * 回复评论
	 */
	public function replyCommentOp(){
		$result = array();
		$p_data = input('param.');
		$data['article_id'] = $p_data['article_id'];
		$data['content'] = $p_data['comment'];
		$data['pid'] = $p_data['pid'];
		$data['member_id'] = session('member_id');
		//$data['member_id'] = $this->getNotMember($p_data['pid']);
		$res = model('Comment')->insertData($data);
		if($res !== false){
			//评论内容信息
			$comment_info = model('Comment')->getInfo(array('id'=>$res),'id,member_id,content,add_time,pid');
			//评论者信息
			$member_info = model('Member')->getInfo(array('id'=>$comment_info['member_id']),'nick_name,head_imgurl');
			//网站配置信息
			$website_info = model('config')->getConfig(array('type'=>'website'));
			//被回复者信息
			$f_user_name = model('Comment')->getCommentMemberName($data['pid']);
			$result['error'] = 0;
			$result['member_info'] = $member_info;
			$result['website_info'] = $website_info;
			$comment_info['add_time'] = date('Y-m-d H:i:s',$comment_info['add_time']);
			$comment_info['content'] = setHtmlContent($comment_info['content']);
			$result['comment_info'] = $comment_info;
			$result['f_user_name'] = $f_user_name;
		}else{
			$result['msg'] = '不好意思，评论丢了！';
			$result['error'] = 1;
		}
		return json($result);
	}
	
	public function getNotMember($pid){
		$comment_info = model('Comment')->getInfo(array('id'=>$pid));
		$member_id = $comment_info['member_id'];
		$member_info = model('Member')->getInfo('id <> '.$member_id,'id');
		return $member_info['id'];
	}
}